package us.nchu.tomtiger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import us.nchu.tomtiger.interceptor.*;

import java.nio.charset.Charset;
import java.util.List;

/**
 * Created by XiongXuan on 2018/1/2.
 * 配置类
 */
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new RegisterInterceptor()).addPathPatterns("/register");
        registry.addInterceptor(new SmsVerifyCodeInterceptor()).addPathPatterns("/SmsVerifyCode");
        registry.addInterceptor(new IndexInterceptor()).addPathPatterns("/");
        registry.addInterceptor(new AllInterceptor()).addPathPatterns("/**");
        registry.addInterceptor(new CenterInterceptor()).addPathPatterns("/center/**");
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/login");
        registry.addInterceptor(new UserShowInterceptor()).addPathPatterns("/user/**");
        registry.addInterceptor(new ProjectInterceptor()).addPathPatterns("/project/**");
        registry.addInterceptor(new AdminInterceptor()).addPathPatterns("/adminASDF/**");
        super.addInterceptors(registry);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/adminASDF/**")
                .addResourceLocations("classpath:/static/");
    }

    @Bean
    public HttpMessageConverter<String> responseBodyConverter() {
        return new StringHttpMessageConverter(Charset.forName("UTF-8"));
    }

    @Override
    public void configureMessageConverters(
            List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        converters.add(responseBodyConverter());
    }

    @Override
    public void configureContentNegotiation(
            ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false);
    }
}
